home *** CD-ROM | disk | FTP | other *** search
- /*
- Probes.c
-
- Handles the actual probing of the host.
-
- 06/27/95 dn - Created.
- 11/28/95 mc - converted to CodeWarrior
- 12/04/95 dn - Prep'd for Apprentice 4
-
- */
-
- #include <SpinLib.h>
- #include <MyTCPIncludes.h>
-
- #if defined(SYMANTEC_C)||defined(THINK_C)
- #else
- #include <SIOUX.h>
- #include <stdio.h>
- #endif
-
- #include "UDP Probe.h"
- #include "Probes.h"
-
- Boolean gProbing=false;
- short gProbes=0;
-
- OSErr InitProbes(void){
- OSErr err=noErr;
-
- // not really much to do...
- err=SpinInit();
-
- gProbes=0;
- gProbing=false;
-
- return err;
- }
-
- OSErr CleanupProbes(void){
- OSErr err=noErr;
-
- if (gProbing){
- // if we are currently probing a host, we need to stop it
- SpinStart(1);
- err=StopProbe();
- SpinStop();
- }
-
- err=SpinCleanup();
-
- return err;
- }
-
- static Boolean gStrToAddrDone;
-
- static pascal void StrToAddrDone(HostInfoPtr hostInfoPtr, char* userDataPtr){
-
- gStrToAddrDone=true;
- }
-
- OSErr StartProbe(DialogPtr dlg){
- OSErr err=noErr;
- Handle h;
- short i;
- Rect r;
- Str255 hostStr, portStr, timeoutStr, textStr;
- ip_addr host;
- short port;
- short timeout;
- char* txt;
-
- // if continuing...
- if (dlg!=(DialogPtr)0L){
- // start spinning the cursor
- SpinStart(1);
-
- // extract information from the dialog...
- GetDItem(dlg,3,&i,&h,&r);
- GetIText(h,hostStr);
-
- GetDItem(dlg,4,&i,&h,&r);
- GetIText(h,portStr);
-
- GetDItem(dlg,5,&i,&h,&r);
- GetIText(h,timeoutStr);
-
- GetDItem(dlg,6,&i,&h,&r);
- GetIText(h,textStr);
-
- // do the required conversions...
- {
- // convert host name to ip_addr
- char* cp;
- HostInfo theHost;
- HostInfoUPP hiupp;
-
- hostStr[hostStr[0]+1]=0;
- cp=(char*)&(hostStr[1]);
-
- gStrToAddrDone=false;
-
- hiupp=NewHostInfoProc(StrToAddrDone);
- err=StrToAddr(cp,&theHost,hiupp,(char*)0L);
-
- if ((err==-1)||(err==cacheFault)){
- // not done yet...
- EventRecord er;
-
- while (!gStrToAddrDone){
- // give the system some time...
- if (WaitNextEvent(everyEvent,&er,100,(RgnHandle)0)){
- if (er.what==keyDown){
- // check for command-period
- char ch;
-
- ch=er.message&charCodeMask;
-
- if ((er.modifiers&cmdKey)&&(ch=='.')){
- // the user wants to cancel this operation...
- HandleError("\pCanceling probe",0);
-
- SpinStop();
- DisposeHostInfoProc(hiupp);
- return noErr;
- }
- } else {
- SysBeep(5); // notify of the error...
- }
- }
- }
- } else if (err!=noErr){
- // error calling StrToAddr
- SpinStop();
- DisposeHostInfoProc(hiupp);
-
- HandleError("\pStrToAddr returns error",err);
-
- return err;
- }
-
- DisposeHostInfoProc(hiupp);
-
- // ah, the name has been converted, just take it out of the HostInfo record...
- host=theHost.addr[0];
- err=theHost.rtnCode;
- }
- {
- // convert port string to a short
- // convert timeout string to a short
- long tval;
-
- StringToNum(portStr,&tval);
- port=LoWord(tval);
-
- StringToNum(timeoutStr,&tval);
- timeout=LoWord(tval);
- }
- {
- // copy text to a c string
- txt=(char*)NewPtr(textStr[0]+2);
- BlockMoveData((Ptr)textStr,(Ptr)txt,textStr[0]+2);
-
- MyP2CStr((StringPtr)txt);
- }
-
- // if no errors handle the probe
- if (err==noErr){
- GetDItem(dlg,11,&i,&h,&r);
-
- if (GetCtlValue((ControlHandle)h)) // do a udp probe
- err=Probe(host,port,timeout,txt);
- else // do a tcp probe
- err=TCPProbe(host,port,timeout,txt);
- }
-
- SpinStop();
- }
-
- return err;
- }
-
- static StreamPtr list[200];
- static short nlist=0;
- static Boolean gDataArrived;
-
- static StreamPtr active[200];
- static short aque=0;
-
- static StreamPtr tlist[200];
- static short tnlist=0;
- static Boolean gtDataArrived;
-
- static StreamPtr tactive[200];
- static short taque=0;
-
- static ip_addr thosts[200];
- static short tports[200];
-
- static pascal void UDPNotify(StreamPtr udp,unsigned short code,Ptr user,ICMPReport* icmp){
-
- if (code==UDPDataArrival){
- list[nlist]=udp;
- nlist++;
-
- gDataArrived=true;
- }
- }
-
- static short gterm=0;
-
- static pascal void TCPNotify(StreamPtr tcp,unsigned short code,Ptr user,unsigned short term,ICMPReport* icmp){
- register short loop=0;
-
- if (code==TCPDataArrival){
- while (loop<tnlist){
- if (tlist[loop]==tcp)
- return;
- loop++;
- }
-
- // if we get here then tcp is not yet in the list...
-
- tlist[tnlist]=tcp;
- tnlist++;
-
- gtDataArrived=true;
- } else if (code==TCPTerminate)
- gterm=term;
- }
-
- OSErr HandleDataStream(StreamPtr sp){
- OSErr err=noErr;
- char* buffer;
- long buflen;
- UDPiopbPtr udp;
- ip_addr host;
- short port;
-
- udp=NewUDPiopbPtr();
-
- udp->udpStream=sp;
- udp->ioCRefNum=gMacTCP;
- udp->csParam.receive.timeOut=5;
- udp->csParam.receive.secondTimeStamp=0;
-
- err=UDP_Read(udp,false);
-
- host=udp->csParam.receive.remoteHost;
- port=udp->csParam.receive.remotePort;
-
- if (err!=noErr){
- DisposeUDPiopbPtr(udp);
- HandleError("\pError receiving UDP data.",err);
- return err;
- }
-
- if (udp->csParam.receive.rcvBuffLen>0){
- // data is available...
- buflen=udp->csParam.receive.rcvBuffLen;
-
- buffer=(char*)NewPtr(buflen+5);
-
- BlockMoveData((Ptr)udp->csParam.receive.rcvBuff,(Ptr)buffer+1,buflen);
-
- } else
- buflen=0L;
-
- err=UDP_BfrReturn(udp,false);
-
- // all data has been read and the buffer has been returned.
- // it is now ok to get rid of the stream.
-
- udp->ioCRefNum=gMacTCP;
- udp->udpStream=sp;
- {
- register short loop=0;
-
- while (loop<200){
- if (active[loop]==sp){
- active[loop]=(StreamPtr)-1;
- break;
- }
- loop++;
- }
- }
-
- UDP_Release(udp,false);
-
- gProbes--;
- if (gProbes==0)
- gProbing=false;
-
- DisposeUDPiopbPtr(udp);
-
- if (err==noErr){
- // Display the text...
- err=ReportData(buffer,buflen,host,port);
- }
-
- return err;
- }
-
- OSErr HandleTCPDataStream(StreamPtr sp){
- OSErr err=noErr;
- char* buffer;
- long buflen;
- TCPiopbPtr tcp;
- rdsEntry rds[5];
- ip_addr host;
- short port;
- short term;
-
- tcp=NewTCPiopbPtr();
-
- // no error, prepare to receive data
- rds[4].length=0;
- rds[4].ptr=(char*)0;
-
- // now fetch the data...
- tcp->tcpStream=sp;
- tcp->ioCRefNum=gMacTCP;
- tcp->ioCompletion=(TCPIOCompletionUPP)0;
- tcp->csParam.receive.commandTimeoutValue=0;
- tcp->csParam.receive.rdsPtr=(char*)rds;
- tcp->csParam.receive.rdsLength=4;
-
- err=TCP_NoCopyRcv(tcp,true);
- err=tcp->ioResult;
-
- if (err!=noErr){
- DisposeTCPiopbPtr(tcp);
- HandleError("\pError receiving TCP data.",err);
- return err;
- }
-
- // the data has been received...
-
- if (rds[0].length>0){
- // data is available...
- buflen=rds[0].length;
-
- buffer=(char*)NewPtr(buflen+5);
-
- BlockMoveData((Ptr)rds[0].ptr,(Ptr)buffer+1,buflen);
-
- } else
- buflen=0L;
-
- tcp->tcpStream=sp;
- tcp->ioCRefNum=gMacTCP;
- tcp->csParam.receive.rdsPtr=(char*)rds;
-
- err=TCP_RcvBfrReturn(tcp,false);
-
- if (err!=noErr)
- HandleError("\pError returning TCP buffers.",err);
- err=noErr;
-
- // all data has been read and the buffer has been returned.
- // it is now ok to get rid of the stream.
-
- tcp->ioCRefNum=gMacTCP;
- tcp->tcpStream=sp;
- {
- register short loop=0;
-
- while (loop<200){
- if (tactive[loop]==sp){
- tactive[loop]=(StreamPtr)-1;
- host=thosts[loop];
- port=tports[loop];
- break;
- }
- loop++;
- }
- }
-
- tcp->tcpStream=sp;
- tcp->ioCRefNum=gMacTCP;
- tcp->csParam.close.ulpTimeoutValue=0;
- tcp->csParam.close.ulpTimeoutAction=0;
- tcp->csParam.close.validityFlags=0;
-
- TCP_Close(tcp,false);
-
- tcp->tcpStream=sp;
- tcp->ioCRefNum=gMacTCP;
-
- TCP_Release(tcp,false);
-
- gProbes--;
- if (gProbes==0)
- gProbing=false;
-
- DisposeTCPiopbPtr(tcp);
-
- if (err==noErr){
- // Display the text...
- err=ReportData(buffer,buflen,host,port);
- }
-
- return err;
- }
-
- OSErr ReportData(char* buffer,short buflen,ip_addr host,short port){
- #if defined(SYMANTEC_C)||defined(THINK_C)
- OSErr err=noErr;
- DialogPtr dlg;
- Handle h;
- short i,num;
- Rect r;
- Str255 sa[4];
- register short loop=0;
- Str255 hostport,temp;
- long val;
-
- while (loop<4){
- if (buflen>0){
- if (buflen>255)
- num=255;
- else
- num=buflen;
-
- // set up the string
- BlockMoveData((Ptr)buffer,(Ptr)&(sa[loop][1]),num);
- sa[loop][0]=num;
-
- // fix the pointers for the next pass
- buffer+=num;
- buflen-=num;
- } else
- sa[loop][0]=0;
- loop++;
- }
-
- hostport[0]=0;
-
- #define AddChar(s,c) s[1+s[0]++]=c
-
- val=host>>24;
- NumToString(val,temp);
- i=0;
- while (i<temp[0]){
- AddChar(hostport,temp[i+1]);
- i++;
- }
- AddChar(hostport,'.');
-
-
- val=(host>>16)&0x000000ff;
- NumToString(val,temp);
- i=0;
- while (i<temp[0]){
- AddChar(hostport,temp[i+1]);
- i++;
- }
- AddChar(hostport,'.');
-
- val=(host>>8) & 0x000000ff;
- NumToString(val,temp);
- i=0;
- while (i<temp[0]){
- AddChar(hostport,temp[i+1]);
- i++;
- }
- AddChar(hostport,'.');
-
- val=host&0x000000ff;
- NumToString(val,temp);
- i=0;
- while (i<temp[0]){
- AddChar(hostport,temp[i+1]);
- i++;
- }
- AddChar(hostport,':');
-
- val=port & 0x000000ff;
- NumToString(val,temp);
- i=0;
- while (i<temp[0]){
- AddChar(hostport,temp[i+1]);
- i++;
- }
-
- dlg=GetNewDialog(129,(Ptr)0L,(WindowPtr)-1);
-
- if (dlg!=(DialogPtr)0){
- GrafPtr save;
-
- GetPort(&save);
-
- SetWRefCon((WindowPtr)dlg,'NebP');
-
- SetPort(dlg);
-
- ParamText(sa[0],sa[1],sa[2],sa[3]);
-
- GetDItem(dlg,4,&i,&h,&r);
- SetIText(h,hostport);
-
- ShowWindow((WindowPtr)dlg);
-
- SetPort(save);
- } else
- err=ResError();
-
- return err;
- #else // using SIOUX & MetroWorks
- printf("Data receved from %d.%d.%d.%d:%d\n",
- (host>>24) & 255,
- (host>>16) & 255,
- (host>> 8) & 255,
- (host) & 255,
- port);
-
- fwrite(buffer, 1, buflen, stdout);
- return noErr;
- #endif
- }
-
- OSErr HandleDataArrivals(void){
- OSErr err=noErr;
- short count=0;
-
- if (gtDataArrived){
- if (tnlist>0){
- SpinStart(-1);
-
- while (count<tnlist){
- err=HandleTCPDataStream(tlist[count]);
-
- if (err!=noErr){
- SpinStop();
- return err;
- }
- count++;
- }
- tnlist=0;
- SpinStop();
- }
- }
- count=0;
-
- if (gDataArrived){
- // some data needs to be received
-
- if (nlist>0){
- SpinStart(-1);
-
- while (count<nlist){
- // handle the stream pointed to by list[count]
- err=HandleDataStream(list[count]);
-
- if (err!=noErr){
- SpinStop();
- return err;
- }
- count++;
- };
-
- nlist=0;
- SpinStop();
- }
- }
-
- return err;
- }
-
- OSErr TCPProbe(ip_addr host,short port,short timeout,char* txt){
- OSErr err=noErr;
- TCPiopbPtr tcp;
- Ptr buffer;
- TCPNotifyUPP notify;
- StreamPtr stream;
- wdsEntry en[3];
- short slen;
-
- tcp=NewTCPiopbPtr();
- buffer=NewPtr(10*1024);
-
- notify=NewTCPNotifyProc(TCPNotify);
-
- tcp->ioCRefNum=gMacTCP;
- tcp->csParam.create.rcvBuff=buffer;
- tcp->csParam.create.rcvBuffLen=10*1024;
- tcp->csParam.create.notifyProc=notify;
-
- err=TCP_Create(tcp,false); // create the stream...
-
- if (err==noErr){
- // then the stream was created...
- stream=tcp->tcpStream;
- tactive[taque]=stream;
- thosts[taque]=host;
- tports[taque++]=port;
-
- if (taque==200)
- taque=0;
-
- // now send the data on the stream...
- slen=StrLen(txt);
-
- en[0].length=slen;
- en[0].ptr=txt;
-
- en[1].length=0;
- en[1].ptr=0;
-
- // now open the connection...
- tcp->ioCRefNum=gMacTCP;
- tcp->ioCompletion=0L;
- tcp->tcpStream=stream;
- tcp->csParam.open.ulpTimeoutValue=0;
- tcp->csParam.open.ulpTimeoutAction=0;
- tcp->csParam.open.remoteHost=host;
- tcp->csParam.open.remotePort=port;
- tcp->csParam.open.validityFlags=0;
- tcp->csParam.open.tosFlags=0;
- tcp->csParam.open.precedence=0;
- tcp->csParam.open.dontFrag=true;
- tcp->csParam.open.timeToLive=0;
- tcp->csParam.open.security=0;
- tcp->csParam.open.optionCnt=0;
-
- err=TCP_ActiveOpen(tcp,false);
-
- if ((err==noErr)&&(slen>0)){
- // then the connection has been opened, send the text....
-
- // refill tcp
-
- tcp->ioCompletion=0L;
- tcp->ioCRefNum=gMacTCP;
- tcp->tcpStream=stream;
- tcp->csParam.send.ulpTimeoutValue=0;
- tcp->csParam.send.ulpTimeoutAction=0;
- tcp->csParam.send.validityFlags=0;
- tcp->csParam.send.pushFlag=true;
- tcp->csParam.send.urgentFlag=false;
- tcp->csParam.send.wdsPtr=(char*)en;
-
- err=TCP_Send(tcp,false); // do it synchronously...
-
- if (err==noErr){
-
- if (tcp->ioResult==noErr){
- // no Errors
- } else
- err=tcp->ioResult;
- }
- }
- }
-
- // everything should have been sent already
- // ok to return...
-
- DisposeTCPiopbPtr(tcp);
-
- gProbing=true;
- gProbes++;
-
- return err;
- }
-
- OSErr Probe(ip_addr host,short port,short timeout,char* txt){
- OSErr err=noErr;
- UDPiopbPtr udp;
- Ptr buffer;
- UDPNotifyUPP notify;
- StreamPtr stream;
- wdsEntry en[3];
-
- udp=NewUDPiopbPtr();
- buffer=NewPtr(10*1024);// allocate a receive buffer
-
- notify=NewUDPNotifyProc(UDPNotify);
-
- udp->ioCRefNum=gMacTCP;
- udp->csParam.create.rcvBuff=buffer;
- udp->csParam.create.rcvBuffLen=10*1024;
- udp->csParam.create.localPort=port;
- udp->csParam.create.notifyProc=notify;
- udp->csParam.create.userDataPtr=(Ptr)SetCurrentA5();
-
- err=UDP_Create(udp,false); // do it synchronously...
-
- if (err==noErr){
- // then the stream was created...
- stream=udp->udpStream;
- active[aque++]=udp->udpStream;
- if (aque==200)
- aque=0;
-
- // now send the data on the stream...
-
- en[0].length=StrLen(txt);
- en[0].ptr=txt;
-
- en[1].length=0;
- en[1].ptr=0;
-
- // refill udp
-
- udp->ioCompletion=0L;
- udp->ioCRefNum=gMacTCP;
- udp->csParam.send.reserved=0;
- udp->csParam.send.remoteHost=host;
- udp->csParam.send.remotePort=port;
- udp->csParam.send.checkSum=true;
- udp->csParam.send.wdsPtr=(char*)en;
- udp->csParam.send.localPort=port;
-
- err=UDP_Write(udp,false); // do it synchronously...
-
- if (err==noErr){
-
- if (udp->ioResult==noErr){
- // then the packet was sent successfully...
- } else
- err=udp->ioResult;
- }
-
- }
-
- // everything should have been sent already
- // ok to return...
- DisposeUDPiopbPtr(udp);
-
- gProbing=true;
- gProbes++;
-
- return err;
- }
-
- OSErr StopProbe(void){
- OSErr err=noErr;
- register short loop=0;
- UDPiopbPtr udp;
-
- udp=NewUDPiopbPtr();
-
- while (loop<200){
- if (active[loop]!=(StreamPtr)-1){
- udp->ioCRefNum=gMacTCP;
- udp->udpStream=active[loop];
-
- err=UDP_Release(udp,false);
- }
- loop++;
- }
-
- DisposeUDPiopbPtr(udp);
-
- return err;
- }
-
-